package cn.sf.sufingvideoconsumer.controller;


import cn.sf.pojo.FileUploadResult;
import cn.sf.pojo.Video;
import cn.sf.sufingvideoconsumer.service.IndexService;
import cn.sf.sufingvideoconsumer.service.VideoService;

import cn.sf.util.FileUpload;
import cn.sf.vo.CommentAll;
import cn.sf.vo.VideoComment;
import cn.sf.vo.VideoDate;
import cn.sf.vo.VideoVo;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.util.List;

/**
 * @author sric
 */

@RestController
@CrossOrigin("*")
public class VideoController {

    @Resource
    private IndexService indexService;

    @Resource
    private FileUpload fileUpload;
/*
    @RequestMapping("/indexVideo")
    public List<Video> indexVideo() {
        String redisQueryRandom = indexService.getRedisQueryRandom();
        List<Video> parseArray = JSON.parseArray(redisQueryRandom, Video.class);
        return parseArray;
    }*/

    @Resource
    private VideoService videoService;

    /**
     * 查询视频表及相应的用户信息
     * @param id    视频ID
     * @return
     */
    @RequestMapping("/queryVideoAndUser")
    public Video queryVideoAndUser(long id){
        return videoService.queryVideoAndUser(id);
    }

    /**
     * 根据分类查询相关视频
     * @param classify  视频分类
     * @return
     */
    @RequestMapping("/queryOtherVideoList")
    public List<Video> queryOtherVideoList(long classify){
        return videoService.queryOtherVideoList(classify);
    }

    /**
     * 点击进入改视频增加一条播放量
     * @param id    视频ID
     * @return
     */
    @RequestMapping("/updVideoPlayNumber")
    public boolean updVideoPlayNumber(long id){
        return videoService.updVideoPlayNumber(id);
    }

    /**
     * 点击点赞为改视频增加一条点赞量
     * @param id
     * @return
     */
    @RequestMapping("/updVideoLikeNumber")
    public boolean updVideoLikeNumber(long id){
        return videoService.updVideoLikeNumber(id);
    }

    /**
     * 点击举报未该视频增加一条举报量
     * @param videoId
     * @return
     */
    @RequestMapping("/updVideoReport")
    public boolean updVideoReport(long videoId){
        return videoService.updVideoReport(videoId);
    }

    @RequestMapping("/queryIsOrNotCollect")
    public boolean queryIsOrNotCollect(long userId, long videoId){
        return videoService.queryIsOrNotCollect(userId, videoId);
    }

    /**
     * 添加收藏:实现事务操作，将为该视频收藏量+1，添加到收藏表中同步执行
     * 取消收藏:实现事务操作，将为该视频收藏量-1，删除收藏表中的数据同步执行
     * @param userId        登录用户ID
     * @param videoId       视频ID
     * @return  返回结果
     * @throws Exception
     */
    @RequestMapping("/VideoCollect")
    public String VideoCollect(@RequestParam("userId") long userId,
                               @RequestParam("videoId") long videoId) throws Exception{
        return videoService.VideoCollect(userId,videoId);
    }

    @RequestMapping("/get")
    public List<VideoVo> get(Integer id){
        return videoService.get(id);
    }


    @RequestMapping("/getAllComment")
    public List<VideoComment> getAllComment(Integer commentVideoId){
        return videoService.getAllComment(commentVideoId);
    }

    @PostMapping("/getVideoAll")
    public String getVideoAll(String videoTitle,Integer videoUserId,Integer pageNo){
        return videoService.getVideoAll(videoTitle,videoUserId,pageNo);
    }


    @PostMapping("/updateVideo")
    private boolean updateVideo(Video video){
        int count=videoService.updateVideo(video);
        if(count>0){
            return true;

        }else{
            return false;
        }

    }
    @PostMapping("/deleteVideo")
    public String deleteVideo(Integer videoId){
        int count=videoService.deleteVideo(videoId);
        if(count>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @RequestMapping("/getVideoDate")
    public List<VideoDate> getVideoDate(Integer userId){
        return videoService.getVideoDate(userId);
    }
    @RequestMapping("/getComment")
    public List<CommentAll> getComment(Integer commentVideoId){
        return videoService.getComment(commentVideoId);
    }


    @PostMapping("/deleteComment")
    public String deleteComment(Integer commentId){
        int count =videoService.deleteComment(commentId);
        if(count>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }


    @RequestMapping("/getVideoIdMAX")
    public int getVideoIdMAX(long id) {
        return videoService.getVideoIdMAX(id);
    }


    @RequestMapping("/addVideo")
    public int addVideo(Video video){
        System.out.println(video.toString());
        System.out.println("上传的视频路径:"+video.getVideoUrl());
        /*
        video.setVideoUrl(uploadFile);
        System.out.println(uploadFile);*/
        int i =videoService.addVideo(video);
        System.out.println(i);
        return 1;
    }

    @RequestMapping("/getUrl")
    public String addVideo(@RequestParam("file") MultipartFile uploadFile){
        System.out.println("文件:" + uploadFile);
        FileUploadResult upload = fileUpload.upload(uploadFile);
        System.out.println("文件在OSS的路径:" + upload.getName());
        return upload.getName();
    }



}
